# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

class Customer(models.Model):
    """客户管理"""
    name = models.CharField("客户名称", max_length=100)
    code = models.CharField("客户编码", max_length=50, unique=True)
    contact = models.CharField("联系人", max_length=50, blank=True)
    phone = models.CharField("电话", max_length=20, blank=True)
    email = models.EmailField("邮箱", blank=True)
    address = models.TextField("地址", blank=True)
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    
    class Meta:
        verbose_name = "客户"
        verbose_name_plural = "客户管理"
    
    def __str__(self):
        return f"{self.name} ({self.code})"

class Product(models.Model):
    """产品管理"""
    name = models.CharField("产品名称", max_length=100)
    code = models.CharField("产品编码", max_length=50, unique=True)
    category = models.CharField("产品类别", max_length=50, blank=True)
    description = models.TextField("产品描述", blank=True)
    unit = models.CharField("单位", max_length=10, default="件")
    price = models.DecimalField("单价", max_digits=10, decimal_places=2, null=True, blank=True)
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    
    class Meta:
        verbose_name = "产品"
        verbose_name_plural = "产品管理"
    
    def __str__(self):
        return f"{self.name} ({self.code})"

class Order(models.Model):
    """订单管理"""
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('processing', '处理中'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
    ]
    
    order_no = models.CharField("订单号", max_length=50, unique=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name="客户")
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name="产品")
    quantity = models.PositiveIntegerField("数量")
    status = models.CharField("状态", max_length=20, choices=STATUS_CHOICES, default='pending')
    order_date = models.DateTimeField("下单时间", auto_now_add=True)
    delivery_date = models.DateField("交货日期", null=True, blank=True)
    notes = models.TextField("备注", blank=True)
    
    class Meta:
        verbose_name = "订单"
        verbose_name_plural = "订单管理"
        ordering = ['-order_date']
    
    def __str__(self):
        return f"{self.order_no} - {self.customer.name}"

class Equipment(models.Model):
    """设备管理"""
    STATUS_CHOICES = [
        ('running', '运行中'),
        ('maintenance', '维护中'),
        ('idle', '空闲'),
        ('fault', '故障'),
    ]
    
    name = models.CharField("设备名称", max_length=100)
    code = models.CharField("设备编码", max_length=50, unique=True)
    model = models.CharField("设备型号", max_length=50, blank=True)
    location = models.CharField("设备位置", max_length=100, blank=True)
    status = models.CharField("设备状态", max_length=20, choices=STATUS_CHOICES, default='idle')
    purchase_date = models.DateField("采购日期", null=True, blank=True)
    last_maintenance = models.DateField("上次维护", null=True, blank=True)
    notes = models.TextField("备注", blank=True)
    
    class Meta:
        verbose_name = "设备"
        verbose_name_plural = "设备管理"
    
    def __str__(self):
        return f"{self.name} ({self.code})"

class Employee(models.Model):
    """员工管理"""
    DEPARTMENT_CHOICES = [
        ('production', '生产部'),
        ('quality', '质量部'),
        ('warehouse', '仓库'),
        ('management', '管理部'),
        ('maintenance', '维护部'),
    ]
    
    name = models.CharField("姓名", max_length=50)
    employee_id = models.CharField("工号", max_length=20, unique=True)
    department = models.CharField("部门", max_length=20, choices=DEPARTMENT_CHOICES)
    position = models.CharField("职位", max_length=50, blank=True)
    phone = models.CharField("电话", max_length=20, blank=True)
    email = models.EmailField("邮箱", blank=True)
    hire_date = models.DateField("入职日期", null=True, blank=True)
    is_active = models.BooleanField("是否在职", default=True)
    
    class Meta:
        verbose_name = "员工"
        verbose_name_plural = "员工管理"
    
    def __str__(self):
        return f"{self.name} ({self.employee_id})"

class WorkOrder(models.Model):
    """工单管理"""
    STATUS_CHOICES = [
        ('created', '已创建'),
        ('assigned', '已分配'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
    ]
    
    work_order_no = models.CharField("工单号", max_length=50, unique=True)
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name="关联订单")
    assigned_to = models.ForeignKey(Employee, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="负责人")
    equipment = models.ForeignKey(Equipment, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="使用设备")
    status = models.CharField("状态", max_length=20, choices=STATUS_CHOICES, default='created')
    start_date = models.DateTimeField("开始时间", null=True, blank=True)
    end_date = models.DateTimeField("结束时间", null=True, blank=True)
    notes = models.TextField("备注", blank=True)
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    
    class Meta:
        verbose_name = "工单"
        verbose_name_plural = "工单管理"
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.work_order_no} - {self.order.product.name}" 